package com.chehongwei.ioc;

import com.chehongwei.aspect.Aspect;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class MyInvocationHandler
    implements InvocationHandler {

    private Object target;
    private Aspect aspect;

    public MyInvocationHandler(
        Object target,
        Aspect aspect) {
        this.target = target;
        this.aspect = aspect;
    }

    @Override
    public Object invoke(
        Object proxy,
        Method method,
        Object[] args)
        throws Throwable {

        if (method.getName().equals(aspect.getPointcut().getMethodPattern())) {
            return aspect.getAdvice().invoke(target, method, args);
        }

        return method.invoke(target, args);
    }
}
